package com.alibaba.algo.controller.user;

import com.alibaba.algo.annotation.OperateLogAnnotation;
import com.alibaba.algo.apiimpl.feign.GroupFeignServiceImpl;
import com.alibaba.algo.cooperation.common.BaseResult;
import com.alibaba.algo.cooperation.feign.vo.req.SelectGroupPageRequestVO;
import com.alibaba.algo.cooperation.feign.vo.req.SysGroupCreateRequestVO;
import com.alibaba.algo.cooperation.feign.vo.req.SysGroupUpdateRequestVO;
import com.alibaba.algo.enums.OperateTypeEnum;
import com.alibaba.algo.enums.ResourceTypeEnum;
import com.xxl.sso.core.conf.Conf;
import com.xxl.sso.core.user.XxlSsoUser;
import io.swagger.annotations.Api;
import org.springframework.util.ObjectUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.multipart.MultipartFile;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.util.List;

/**
 * @author Fan.tianpeng
 * @date 2021/5/7 19:42
 */
@RestController
@RequestMapping(value = "/usercenter-admin-app/admin/sysGroup")
@Api(value = "用户组管理", tags = "用户组管理")
public class SysGroupController {

    @Resource
    private GroupFeignServiceImpl sysGroupService;

    @PostMapping(value = "insert")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.GROUP,operateType = OperateTypeEnum.INSERT)
    BaseResult insert(@RequestBody SysGroupCreateRequestVO vo, HttpServletRequest request) {
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if(null != xxlSsoUser) {
            vo.setUserId((ObjectUtils.isEmpty(xxlSsoUser.getUserid()) ? 0L : Long.parseLong(xxlSsoUser.getUserid())));
            vo.setUserName(xxlSsoUser.getUsername());
        }
        return sysGroupService.insert(vo);
    }

    @PostMapping(value = "update")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.GROUP,operateType = OperateTypeEnum.UPDATE)
    BaseResult update(@RequestBody SysGroupUpdateRequestVO vo, HttpServletRequest request) {
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        if(null != xxlSsoUser) {
            vo.setUserId((ObjectUtils.isEmpty(xxlSsoUser.getUserid()) ? 0L : Long.parseLong(xxlSsoUser.getUserid())));
            vo.setUserName(xxlSsoUser.getUsername());
        }
        return sysGroupService.update(vo);
    }

    @PostMapping(value = "delete")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.GROUP,operateType = OperateTypeEnum.DELETE)
    BaseResult delete(@RequestBody List<Long> vo) {
        return sysGroupService.delete(vo);
    }

    @PostMapping(value = "enable")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.GROUP,operateType = OperateTypeEnum.UPDATE)
    BaseResult enable(@RequestBody List<Long> vo, HttpServletRequest request) {
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        String userName = null;
        if(null != xxlSsoUser) {
            userName = xxlSsoUser.getUsername();
        }
        return sysGroupService.enable(vo,userName);
    }

    @PostMapping(value = "disable")
    @OperateLogAnnotation(resouceType = ResourceTypeEnum.GROUP,operateType = OperateTypeEnum.UPDATE)
    BaseResult disable(@RequestBody List<Long> vo, HttpServletRequest request) {
        XxlSsoUser xxlSsoUser = (XxlSsoUser) request.getAttribute(Conf.SSO_USER);
        String userName = null;
        if(null != xxlSsoUser) {
            userName = xxlSsoUser.getUsername();
        }
        return sysGroupService.disable(vo,userName);
    }

    @PostMapping(value = "selectPage")
    BaseResult selectPage(@RequestBody SelectGroupPageRequestVO vo) {
        return sysGroupService.selectPage(vo);
    }

    @PostMapping(value = "selectEmployeeDetailPageByGroupId")
    BaseResult selectEmployeeDetailPageByGroupId(@RequestBody SelectGroupPageRequestVO vo) {
        return sysGroupService.selectEmployeeDetailPageByGroupId(vo);
    }

    @PostMapping(value = "import")
    BaseResult importExcel(MultipartFile file) {
        return sysGroupService.importExcel(file);
    }
}
